package com.sm.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sm.dao.IStudentDao;
import com.sm.model.Student;

@Controller
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private IStudentDao iStudentDao;
	
	/**
	 * 添加学生信息
	 */
	@RequestMapping(value = "addstudent" , method = RequestMethod.GET)
	public ModelAndView addStudent(HttpServletRequest req) {
		String mainPage = "studentAdd.jsp";
		req.setAttribute("mainPage", mainPage);
		ModelAndView modelAndView = new ModelAndView("main");
		return modelAndView;
	}
	
	/**
	 * 修改学生信息
	 */
	@RequestMapping(value = "stupdate" , method=  RequestMethod.POST)
	public void stUpdate(HttpServletRequest request,HttpServletResponse response) {
		long stId = Long.parseLong(request.getParameter("stId"));
		String stName = request.getParameter("stName");
		String stSex = request.getParameter("stSex");
		Long stAge = Long.parseLong(request.getParameter("stAge"));
		String stTel = request.getParameter("stTel");
		String stDept = request.getParameter("stDept");
		String stAddress = request.getParameter("stAddress");
		
		Student stu = new Student(stId,stName,stAge,stSex,stDept,stTel,stAddress);
		
		
		try {
			iStudentDao.updateStudent(stu);
			
			response.sendRedirect("/studentManager/student/showstudent");
			//request.getRequestDispatcher("/student/showstudent").forward(request, response);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	/**
	 * 修改学生信息
	 */
	@RequestMapping(value = "updatestudent" , method = RequestMethod.GET)
	public ModelAndView updateStudent(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("main");
		
		String id = request.getParameter("id");
		Student student= iStudentDao.findStudentById(id);
		request.setAttribute("student", student);
		String mainPage = "studentUpdate.jsp";
		request.setAttribute("mainPage", mainPage);
		
		return modelAndView;
	}
	
	/**
	 * 学生信息的单个查询
	 */
	@RequestMapping(value = "getstudent" , method = RequestMethod.POST) 
	public ModelAndView getStudent(HttpServletRequest request) {
		 String id=request.getParameter("searchId");
		 
		 Student student= iStudentDao.findStudentById(id);
		 request.setAttribute("student", student);
		 String mainPage = "showstudent.jsp";
		 request.setAttribute("mainPage", mainPage);
		 
		 return new ModelAndView("main");
	}
	
	/**
	 * 删除学生信息
	 */
	@RequestMapping(value = "deletestudent", method = RequestMethod.GET)
	public void deleteStudent(HttpServletRequest request,HttpServletResponse response) {
		//ModelAndView modelAndView = new ModelAndView();
		
		try {
			Integer id = Integer.valueOf(request.getParameter("id"));
			iStudentDao.delete(id);
			
			request.getRequestDispatcher("/student/showstudent").forward(request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//return modelAndView;
	}
	
	/**
	 * 展示所有学生信息
	 */
	@RequestMapping(value = "showstudent", method = RequestMethod.GET) 
	public ModelAndView showStudent(HttpServletRequest request) {
		
		ModelAndView modelAndView = new ModelAndView("main");
		
		List<Student> list = iStudentDao.selectStudent();
		String mainPage="displaystudent.jsp";
		
		request.setAttribute("list", list);
		request.setAttribute("mainPage", mainPage);
		
		return modelAndView;
	}
	
	/**
	 * 添加学生信息
	 */
	@RequestMapping(value = "stadd", method = RequestMethod.POST)
	public ModelAndView stAdd(HttpServletRequest request) {
		String stName = request.getParameter("stName");
		String stSex = request.getParameter("stSex");
		Long stAge = Long.parseLong(request.getParameter("stAge"));
		String stTel = request.getParameter("stTel");
		String stDept = request.getParameter("stDept");
		String stAddress = request.getParameter("stAddress");
		
		Student stu = new Student(stName,stAge,stSex,stDept,stTel,stAddress);
		
		ModelAndView modelAndView = new ModelAndView();
		
		try {
			
			iStudentDao.addStudent(stu);
			request.setAttribute("msg", "添加成功!!");
			modelAndView.setViewName("main");

		} catch (Exception e) {
			
			modelAndView.setViewName("main");
			request.setAttribute("msg", "添加失败!!");
		}
	
		return modelAndView;
	}
}
